﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.MainBoundedContext.StockModule;
using FluentValidation.Results;
using FluentValidation;

namespace Huirui.Cavan.Domain.MainBoundedContext.PurchaseModule
{
    public class PurchaseModuleFactory
    {
        public static PurchaseOrder CreatePurchaseOrder(string id, DateTime purchaseDate, DateTime deliveryDate, string currency, decimal currencyRate, string supplierInvoiceNumber, string deliveryAddress, string remark, DateTime createDate, DateTime modifyDate, int editUser, Corporation corporation, Employee purchaseEmployee, Project project, Supplier supplier,
            SystemIdentifier.CheckedStatus checkedStatus, PurchaseIdentifier.StockInStatus stockInStatus, Employee checkEmployee, WareHouse wareHouse, string contact, string contactTelephone)
        {
            PurchaseOrder purchaseOrder = new PurchaseOrder(id, purchaseDate, deliveryDate, currency, currencyRate, supplierInvoiceNumber, deliveryAddress, remark, createDate, modifyDate, editUser,
                corporation, purchaseEmployee, project, supplier, checkedStatus, stockInStatus, checkEmployee, wareHouse, contact, contactTelephone);

            return purchaseOrder;
        }

        public static PurchaseOrderDetail CreatePurchaseOrderDetail(decimal number, decimal unitCost, decimal discountRate, decimal discountAmount, decimal purchaseAmount, decimal wholeAmount, decimal taxRate, decimal taxAmount, string description, DateTime createDate, DateTime modifyDate, int editUser, Stock stock, WareHouse wareHouse)
        {
            PurchaseOrderDetail purchaseOrderDetail = new PurchaseOrderDetail(number, unitCost, discountRate, discountAmount, purchaseAmount, wholeAmount, taxRate, taxAmount, description, createDate, modifyDate,
                editUser, stock, wareHouse);

            return purchaseOrderDetail;
        }

        public static Purchase CreatePurchase(string id, DateTime purchaseDate, Project project, Supplier supplier, string receivingNumber, string currency, decimal currencyRate, decimal taxRate, 
            string supplierInvoiceNumber, string deliveryAddress, string remark, string invoiceDescription, decimal discount, decimal finalAmount, 
            SystemIdentifier.PayType payType, PurchaseIdentifier.PayCondition payCondition, DateTime? payDeadline,
            DateTime createDate, DateTime modifyDate, int editUser, Corporation corporation, Employee purchaseEmployee, PurchaseOrder purchaseOrder, WareHouse wareHouse, 
            string contact, string contactTelephone)
        {
            Purchase purchase = new Purchase(id, purchaseDate, project, supplier, receivingNumber, currency, currencyRate, taxRate, supplierInvoiceNumber, deliveryAddress, remark, invoiceDescription, discount, finalAmount, payType, payCondition, payDeadline, createDate,
                modifyDate, editUser, corporation, purchaseEmployee, purchaseOrder, wareHouse, contact, contactTelephone);

            PurchaseValidator validator = new PurchaseValidator();
            ValidationResult results = validator.Validate(purchase);
            if (!results.IsValid)
            {
                throw new ValidationException(results.Errors);
            }

            return purchase;
        }

        public static PurchaseReturn CreatePurchaseReturn(string id, DateTime returnDate, string supplierInvoiceNumber, string currency, decimal currencyRate, decimal taxRate, 
            string receivingNumber, string invoiceDescription, string remark, string deliveryAddress, DateTime createDate, DateTime modifyDate, UserAccount editUser, 
            Corporation corporation, Employee purchaseEmployee, Project project, Supplier supplier, StockInOut stockIn,
            SystemIdentifier.CheckedStatus checkedStatus, WareHouse wareHouse, string contact, string contactTelephone, Purchase purchase)
        {
            PurchaseReturn purchaseReturn = new PurchaseReturn(id, returnDate, supplierInvoiceNumber, currency, currencyRate, taxRate, receivingNumber, invoiceDescription, remark,
                deliveryAddress, createDate, modifyDate, editUser, corporation, purchaseEmployee, project, supplier, stockIn, checkedStatus, wareHouse, contact, contactTelephone, purchase);

            return purchaseReturn;
        }

        public static Pay CreatePay(string id, DateTime payDate, Account fromAccount, string accountNumber, decimal amount, decimal discount, string currency, decimal currencyRate, string remark, DateTime createDate, DateTime modifyDate, UserAccount editUser, Corporation corporation, Employee payEmployee, Supplier supplier)
        {
            Pay purchsePay = new Pay(id, payDate, fromAccount, accountNumber, amount, discount, currency, currencyRate, remark, createDate, modifyDate, editUser,
                corporation, payEmployee, supplier);

            return purchsePay;
        }

        public static UnPay CreateUnPay(string unPayCode, PurchaseIdentifier.UnPayType unPayType, decimal discount, decimal finalAmount, decimal leftAmount,
            SystemIdentifier.PayType payType, PurchaseIdentifier.PayCondition payCondition, Account payAccount, DateTime? payDeadline, string currency, decimal currencyRate,
            DateTime createDate, DateTime modifyDate, int editUser, Corporation corporation, Supplier supplier)
        {
            UnPay purchsePay = new UnPay(unPayCode, unPayType, discount, finalAmount, leftAmount, payType, payCondition, payAccount, payDeadline,currency, currencyRate,
                createDate, modifyDate, editUser, corporation, supplier);

            return purchsePay;
        }

        public static PayDetail CreatePayDetail(Pay pay, UnPay unPay, decimal amount, DateTime certificateDate, DateTime createDate, DateTime modifyDate, int editUser)
        {
            PayDetail payDetail = new PayDetail(pay, unPay, amount, certificateDate, createDate, modifyDate, editUser);

            return payDetail;
        }
    }
}
